package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.ActivityDao;
import cn.edu.sdjzu.xg.kcsj.domain.Activity;
import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.security.User;
import cn.edu.sdjzu.xg.kcsj.util.Condition;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import cn.edu.sdjzu.xg.kcsj.util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.TreeSet;

public class ActivityService {
    private static ActivityDao activityDao= ActivityDao.getInstance();
    private static ActivityService activityService=new ActivityService();
    private ActivityService(){}

    public static ActivityService getInstance(){
        return activityService;
    }

    public Collection<Activity> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException, BysjException {
        return activityDao.findAll(pagination, conditionList);
    }

    public Collection<Activity> getAll(User user) throws SQLException, BysjException {
        Collection<Activity> activities = new HashSet<Activity>();
        for(Activity activity: activityDao.findAll()){
            if(activity.getTeacher()==user){
                activities.add(activity);
            }
        }
        return activities;
    }


    public Activity find(Integer id) throws SQLException, BysjException {
        return activityDao.find(id);
    }

    public boolean update(Activity activity) throws SQLException {
        return activityDao.update(activity);
    }

    public boolean add(Activity activity) throws SQLException {
        return activityDao.add(activity);
    }

    public boolean delete(int id) throws SQLException, BysjException {
        Activity activity = this.find(id);
        return activityDao.delete(activity);
    }

    public boolean delete(Activity activity) throws SQLException {
        return activityDao.delete(activity);
    }

    public Collection<Activity> findByTI(int id) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        return activityDao.findByTI(id,connection);

//            //提交事务
//            connection.commit();
//        }catch (SQLException e){
//            //回滚
//            connection.rollback();
//        } catch (BysjException e) {
//            e.printStackTrace();
//        } finally {
//            //将自动提交设为true，结束事务
//            connection.setAutoCommit(true);
//            JdbcHelper.close(connection);
//        }
//        return activities;
    }
}
